package me.firstapp.api.bbs;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.NotificationJson;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.bbs.BbsNotification;
import me.firstapp.module.bbs.ShopNotification;
import me.firstapp.module.bbs.SystemNotification;
import me.firstapp.service.bbs.NotificationService;

@Controller
@RequestMapping("/notification")
@Api(value = "/notification", description = "通知相关接口")
public class NotificationApi extends BaseApi {

	@Autowired
	private NotificationService notificationService;

	@RequestMapping(value = "findMemberUnReadNotificationCounts.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取用户未读取通知数量", notes = "获取用户未读取通知数量")
	public void findMemberUnReadNotificationCounts(Long memberId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<NotificationJson> resultJsonObject = new SingleObject<NotificationJson>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			NotificationJson notificationJson = notificationService.findUnReadNotificationCounts(memberId, apiClientId);
			resultJsonObject.setObject(notificationJson);
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}

	@RequestMapping(value = "findMemberBbsNotificationPage.do", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取社区通知", notes = "分页获取社区通知")
	public void findMemberBbsNotificationPage(
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, Long memberId,
			Integer isRead, HttpServletRequest request, HttpServletResponse response) {
		PageObject<BbsNotification> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<BbsNotification> bbsNotificationPage = notificationService.findMemberBbsNotificationPage(pageNo,
					pageSize, memberId, isRead, apiClientId);
			JsonPage<BbsNotification> jsonPage = new JsonPage<>();
			jsonPage.setList(bbsNotificationPage.getEntities());
			jsonPage.setPageNo(bbsNotificationPage.getPageNo());
			jsonPage.setPageSize(bbsNotificationPage.getPageSize());
			jsonPage.setTotalCount(bbsNotificationPage.getEntityCount());

			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findMemberSysNotificationPage.do", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取系统通知", notes = "分页获取系统通知")
	public void findMemberSysNotificationPage(
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, Long memberId,
			Integer isRead, HttpServletRequest request, HttpServletResponse response) {
		PageObject<SystemNotification> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<SystemNotification> sysNotificationPage = notificationService.findSysNotificationPage(pageNo, pageSize,
					memberId, isRead, apiClientId);
			JsonPage<SystemNotification> jsonPage = new JsonPage<>();
			jsonPage.setList(sysNotificationPage.getEntities());
			jsonPage.setPageNo(sysNotificationPage.getPageNo());
			jsonPage.setPageSize(sysNotificationPage.getPageSize());
			jsonPage.setTotalCount(sysNotificationPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findMemberShopNotificationPage.do", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取店铺通知", notes = "分页获取店铺通知")
	public void findMemberShopNotificationPage(
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, Long memberId,
			Integer isRead, HttpServletRequest request, HttpServletResponse response) {
		PageObject<ShopNotification> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<ShopNotification> shopNotificationPage = notificationService.findShopNotificationPage(pageNo, pageSize,
					memberId, isRead, apiClientId);
			JsonPage<ShopNotification> jsonPage = new JsonPage<>();
			jsonPage.setList(shopNotificationPage.getEntities());
			jsonPage.setPageNo(shopNotificationPage.getPageNo());
			jsonPage.setPageSize(shopNotificationPage.getPageSize());
			jsonPage.setTotalCount(shopNotificationPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "setNotificationReaded.do", method = RequestMethod.POST)
	@ApiOperation(value = "通知设为已读", notes = "通知设为已读")
	public void setNotificationReaded(Long memberId, String type, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			notificationService.executeSetNotificationReaded(memberId, type, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

}
